package cn.yiueil.convert;

import org.springframework.core.convert.converter.Converter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 1、自定义LocalDateTime日期转换类
 * 2、通过spring在ConversionServiceFactoryBean中注册
 */
public class LocalDateConvert implements Converter<String, LocalDateTime> {
    private String pattern;

    @Override
    public LocalDateTime convert(String source) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(source, dateTimeFormatter);
    }

    @Override
    public <U> Converter<String, U> andThen(Converter<? super LocalDateTime, ? extends U> after) {
        return Converter.super.andThen(after);
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }
}
